package com.example.enterprisemanager.mvvm.fragment.account.register;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.enterprisemanager.BR;
import com.example.enterprisemanager.R;
import com.example.enterprisemanager.base.BaseBackFragment;
import com.example.enterprisemanager.databinding.FragmentRegisterBinding;
import com.example.enterprisemanager.mvvm.fragment.account.login.LoginFragment;


/**
 * Created by YoKeyword on 16/2/14.
 */
public class RegisterFragment extends BaseBackFragment<FragmentRegisterBinding, RegisterViewModel> {

    private LoginFragment.OnLoginSuccessListener mOnLoginSuccessListener;

    public static RegisterFragment newInstance() {
        Bundle args = new Bundle();
        RegisterFragment fragment = new RegisterFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof LoginFragment.OnLoginSuccessListener) {
            mOnLoginSuccessListener = (LoginFragment.OnLoginSuccessListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnLoginSuccessListener");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_register;
    }

    @Override
    protected Class<RegisterViewModel> getModelClass() {
        return RegisterViewModel.class;
    }

    @Override
    protected int getVariableId() {
        return BR.viewModel;
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        mViewModel.sendData.observe(this, (data) -> {
            // 注册成功
            mOnLoginSuccessListener.onLoginSuccess(data);
            popTo(LoginFragment.class, true);
        });
    }

    @Override
    protected void initView() {
        super.initView();

        showSoftInput(mBinding.etAccount);

        mBinding.toolbar.setTitle(R.string.register);
        initToolbarNav(mBinding.toolbar);
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        hideSoftInput();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mOnLoginSuccessListener = null;
    }

}
